package com.jstyle.jclifexd.view;



import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.Xfermode;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.app.MyApplication;
import com.jstyle.jclifexd.utils.ScreenUtils;
import com.jstyle.jclifexd.utils.Utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.Locale;

/**
 * 圆形进度条
 * 
 * @author Administrator
 *
 */
public class CircleProgress extends View {
	private Paint progressPaint;//进度条画笔
	private int bigSqure;//进度条圆半径
	private int strokeWidth;

	private RectF rectF;
	private float endAngel = 0;
	float startAngle=135;
	private int width, height;
	private Paint ballPaint;//小球画笔
	private int textSize,smallTextSize,normalTextSize;
	private Paint bgCiclePaint;//背景圆画笔
	Paint textPaint;
	private String label;

	private int defaultWidth;
	private int defaultHeight;
	private float circleRadius;
	private Bitmap stepBitmap;
	private float progress;
	private String progressString="0%";
	private NumberFormat numberFormat;

	public CircleProgress(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		TypedArray array=context.obtainStyledAttributes(attrs,R.styleable.mCicleProgress);
		width = ScreenUtils.getScreenWidth(context) / 2;
		bigSqure = ScreenUtils.dip2px(context, 75);
		circleRadius =ScreenUtils.dip2px(context, 4);
		height = ScreenUtils.dip2px(context, 30) ;
		strokeWidth = ScreenUtils.dip2px(context, 15);
		label=context.getString(R.string.Steps);
		defaultHeight = ScreenUtils.dip2px(context, 190);
		defaultWidth = ScreenUtils.dip2px(context, 190);
		textSize = ScreenUtils.dip2px(context, 24);
		normalTextSize = ScreenUtils.dip2px(context, 17);
		smallTextSize = ScreenUtils.dip2px(context, 12);
		stepBitmap= BitmapFactory.decodeResource(getResources(),R.drawable.circle_step);

		init();
	}

	private void init() {
		 numberFormat=NumberFormat.getNumberInstance(Locale.ENGLISH);
		 numberFormat.setGroupingUsed(false);
		 numberFormat.setRoundingMode(RoundingMode.DOWN);
		 numberFormat.setMaximumFractionDigits(0);

		// TODO Auto-generated method stub

		rectF = new RectF();
		progressPaint = new Paint();
		progressPaint.setColor(getResources().getColor(
				R.color.tv_homedate_bg));
		progressPaint.setStyle(Paint.Style.STROKE);
		progressPaint.setAntiAlias(true);
		progressPaint.setStrokeWidth(strokeWidth);
		progressPaint.setStrokeCap(Paint.Cap.ROUND);



		bgCiclePaint = new Paint();
		bgCiclePaint.setColor(getResources().getColor(
				R.color.progress_bg_color));
		bgCiclePaint.setAntiAlias(true);
		bgCiclePaint.setStyle(Paint.Style.STROKE);
		bgCiclePaint.setStrokeWidth(strokeWidth);
		bgCiclePaint.setStrokeCap(Paint.Cap.ROUND);

		ballPaint=new Paint();
		ballPaint.setColor(getResources().getColor(
				R.color.progress_bg_color));
		ballPaint.setAntiAlias(true);

		textPaint=new Paint();
		textPaint.setColor(Color.WHITE);
        Typeface mFace;
        if(!Utils.IsRussian(MyApplication.instance())) {
            mFace = Typeface.createFromAsset(getContext().getAssets(),"Brandon_reg.otf");
        }else{
            mFace= Typeface.createFromAsset(getContext().getAssets(), "SourceHanSansCN-Regular.ttf");
        }
        textPaint.setTypeface(mFace);
		textPaint.setAntiAlias(true);
		textPaint.setTextAlign(Paint.Align.CENTER);
		textPaint.setTextSize(textSize);
	}

	private String topTxt = "步数";
	private String bottomTxt = "0";

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);

		rectF.set(0+strokeWidth, 0+strokeWidth, getWidth()-strokeWidth, getHeight()-strokeWidth);
		drawBgArc(canvas);
		drawProgress(canvas);
		drawCicle(canvas);
		drawText(canvas);
		drawBitMap(canvas);
	}

	private void drawBitMap(Canvas canvas) {
		float x=getWidth()/2-stepBitmap.getWidth()/2;
		float y=stepBitmap.getHeight()+strokeWidth/2;
		canvas.drawBitmap(stepBitmap,x,y,textPaint);
	}

	private void drawText(Canvas canvas) {
		float x=getWidth()/2;
		float y=getHeight()/2;
		textPaint.setColor(Color.BLACK);
		textPaint.setTextSize(textSize);
		Rect rect = new Rect();
		String stepValue=bottomTxt;
		textPaint.getTextBounds(stepValue, 0, stepValue.length(), rect);
		int height = rect.height();
		int width=rect.width();
		canvas.drawText(stepValue,x,y+height/2,textPaint);

		String stepLabel=label;
		textPaint.setTextSize(smallTextSize);
		textPaint.getTextBounds(stepLabel, 0, stepLabel.length(), rect);
		int heightLabel = rect.height();
		int widthLabel=rect.width();
		canvas.drawText(stepLabel,x,y+height+heightLabel,textPaint);

		textPaint.setTextSize(normalTextSize);
		String percentValue= progressString.split("%")[0].trim();
		if(Float.valueOf(percentValue)>=100){
			textPaint.setColor(Color.parseColor("#f37864"));
		}
		textPaint.getTextBounds(percentValue, 0, percentValue.length(), rect);
		int heightPercent= rect.height();
		int widthPercent=rect.width();
		canvas.drawText(percentValue,x,getHeight()-heightPercent-strokeWidth,textPaint);

		String percentLabel="%";
		textPaint.setTextSize(smallTextSize);
		textPaint.getTextBounds(percentLabel, 0, percentLabel.length(), rect);
		int widthPercentLabel=rect.width();
		canvas.drawText(percentLabel,x+widthPercent+widthPercentLabel/2,getHeight()-heightPercent-strokeWidth,textPaint);
	}

	private void drawProgress(Canvas canvas) {
		canvas.drawArc(rectF, startAngle, endAngel, false, progressPaint);
	}

	private void drawBgArc(Canvas canvas) {
		canvas.drawArc(rectF, startAngle, 270, false, bgCiclePaint);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		int width = MeasureSpec.getSize(widthMeasureSpec);
		int height = MeasureSpec.getSize(heightMeasureSpec);
		if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {
			height = defaultHeight;
		}
		if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.AT_MOST) {
			width = defaultWidth;
		}
		setMeasuredDimension(width, height);

	}

	private void drawCicle(Canvas canvas) {
		// TODO Auto-generated method stub
		float x = 0;
		float y = 0;
		float sin = (float) Math.sin(46 * Math.PI / 180);
		float cos = (float) Math.cos(46 * Math.PI / 180);
		float centerX=getWidth()/2;
		float centerY=getHeight()/2;
		x = sin * (centerX-strokeWidth);
		y = cos * (centerX-strokeWidth);
		canvas.drawCircle(centerX - x,   centerY+y, circleRadius, ballPaint);
	}

	public void setProgress(float progress) {
		endAngel = 270f * progress;
		postInvalidate();
	}

	private float start = 0.0f;

	public void startAnim(float end) {
		if (start == end)
			return;
		ObjectAnimator animator = ObjectAnimator.ofFloat(this, "progress",
				start, end);
		start = end;

		AnimatorSet animatorSet = new AnimatorSet();
		animatorSet.setDuration(800);
		animatorSet.setInterpolator(new DecelerateInterpolator());
		animatorSet.setTarget(this);
		animatorSet.play(animator);
		animatorSet.start();

	}

	public void setBottomText(int step) {
		this.bottomTxt = String.valueOf(step);
		postInvalidate();
	}

	public void update(int step, float progress) {
		this.bottomTxt = String.valueOf(step);
		endAngel = 270f * progress;
		postInvalidate();
	}
	private int lastStep;
	public void updateGoal(int step,float goal) {
		lastStep=step;
		updateText(step, goal);
	}

	private static final String TAG = "CircleProgress";
	private void updateText(int step,float goal){
		float progress=div(step,goal,2);
		if(progress>0&&progress<0.01)progress=0.01f;
		progressString=numberFormat.format(progress*100)+"%";
		this.bottomTxt = String.valueOf(step);
		endAngel = 270f * progress;
		if(endAngel>270)endAngel=270;
		postInvalidate();
	}
	public  float div(double v1, double v2, int scale) {
		if (scale < 0) {
			throw new IllegalArgumentException(
					"The scale must be a positive integer or zero");
		}
		BigDecimal b1 = new BigDecimal(Double.toString(v1));
		BigDecimal b2 = new BigDecimal(Double.toString(v2));
		return b1.divide(b2, scale, BigDecimal.ROUND_DOWN).floatValue();
	}
	public void updateGoal(float goal) {

		update(lastStep,lastStep/goal);
	}

//	public void changeCicleColor(int color) {
//		progressPaint.setColor(color);
//		ballPaint.setColor(color);
//		ballPaint.setShadowLayer(blurWidth, 0, 0, color);
//		postInvalidate();
//	}

	public void update(int sleepTime, String bottomString, String topString) {
		this.bottomTxt = bottomString;
		this.topTxt = topString;
		postInvalidate();
	}

	public int getLastStep(){
		return this.lastStep;
	}
}
